// pages/KindIndex/kind/kind.js
const KindHttp = require('../../../utils/KindHttp');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        kinds: ['新鲜蔬菜', '水果生鲜', '肉禽蛋品', '酒水饮品', '休闲零食', '粮油调味', '个护清洁', '日用百货', '花卉绿植'],
        sliderIndex: 0,
        shopNum: ""
    },
    onChange(event) {
        wx.setStorageSync('homeToKindById', event.detail)
        this.setData({
            sliderIndex: event.detail
        })
    },
    handleShop() {
        wx.switchTab({
            url: '/pages/ShopIndex/shopping/shopping',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        if (wx.getStorageSync('homeToKindById')) {
            this.setData({
                sliderIndex: wx.getStorageSync('homeToKindById')
            })
        };

        wx.showLoading({
            title: '加载中...',
        })
        // 请求用户收藏表
        var userCollRes = await KindHttp.getUserCollect();
        // console.log(userCollRes.data.res);
        // 请求商品表
        var vegetablesRes = await KindHttp.getVegetables();
        var fruterRes = await KindHttp.getFruter();
        var meatRes = await KindHttp.getMeat();
        var drinkRes = await KindHttp.getDrink();
        var snacksRes = await KindHttp.getSnacks();
        var grainRes = await KindHttp.getGrainAndOil();
        var cleanRes = await KindHttp.getClean();
        var dailyRes = await KindHttp.getDailySupplies();
        var flowerRes = await KindHttp.getFlower();
        this.setData({
            vegetables: vegetablesRes.data.res,
            fruter: fruterRes.data.res,
            meat: meatRes.data.res,
            drink: drinkRes.data.res,
            snacks: snacksRes.data.res,
            grains: grainRes.data.res,
            cleans: cleanRes.data.res,
            dailys: dailyRes.data.res,
            flowers: flowerRes.data.res,
            // 用户表收藏长度
            shopNum: userCollRes.data.res.length
        })
        wx.hideLoading({})
    },
    async onClick(e) {
        var { aid, collect } = e.currentTarget.dataset
        if (collect === false) {
            wx.request({
                url: 'http://122.112.142.6:8888/api/kindCollect',
                data: {
                    id: aid,
                    collect: true
                },
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: async (res) => {
                    if (res.data.msg == "收藏成功" && res.data.status == 200) {
                        wx.showToast({
                            title: '加入购物车成功 !',
                            icon: 'none',
                        })
                    } else {
                        wx.showToast({
                            title: '已经在购物车中了',
                            icon: 'none',
                        })
                    }
                    var userCollRes = await KindHttp.getUserCollect();
                    // console.log(userCollRes.data.res);
                    this.setData({
                        shopNum: userCollRes.data.res.length
                    })
                    wx.setTabBarBadge({
                        index: 2,
                        text: String(this.data.shopNum),
                    })
                },
            })

        } else {
            // wx.request({
            //     url: 'http://122.112.142.6:8888/api/kindCollect',
            //     data: {
            //         id: aid,
            //         collect: false
            //     },
            //     header: {
            //         "Content-Type": "application/x-www-form-urlencoded"
            //     },
            //     method: 'POST',
            //     success: (res) => {
            //         if (res.data.msg == "取消收藏") {
            //             let removeToPull = true;
            //             this.triggerEvent('removeToPull', removeToPull)
            //             wx.showToast({
            //                 title: '取消加入成功 !',
            //                 icon: 'none',
            //             })
            //         }
            //     },
            // })
            wx.showToast({
                title: '已经在购物车中了',
                icon: 'none',
            })

        }
    },
    // 商品详情页
    handleProductDetail(e) {
        var { aid } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/ProductDetail/ProductDetail?id=${aid}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        var userCollRes = await KindHttp.getUserCollect();
        // console.log(userCollRes.data.res);
        this.setData({
            shopNum: userCollRes.data.res.length
        })
        wx.setTabBarBadge({
            index: 2,
            text: String(this.data.shopNum),
        })
        if (wx.getStorageSync('homeToKindById')) {
            this.setData({
                sliderIndex: wx.getStorageSync('homeToKindById')
            })
        };
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})